<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/10/9
  Time: 13:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%--导入css样式--%>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/pintuer.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
<head>
    <title>授课信息管理</title>
</head>
<body>
<div>
    <button class="button" onclick="addTeachInfo()">新增授课信息</button><br>
</div>
<div class="margin">
    <table class="table">
        <thead>
        <tr>
            <td>ID</td>
            <td>教师</td>
            <td>课程</td>
            <td>教室</td>
            <td>上课时间</td>
            <td>操作</td>
        </tr>
        </thead>
        <tbody id="tb">

        </tbody>
    </table>
</div>
<%--分页--%>
<div class="margin">
    每页显示<select id="pageSizeSelect" onchange="onSelectChange()">
    <option value="2" selected="selected">2</option>
    <option value="5">5</option>
    <option value="10">10</option>
</select>条数据，当前是第<span id="currentPageSpan" onchange="onChange()"></span> 页，
    总共<span id="totalPageSpan" onchange="Allpage()"></span> 页
    <button class="button" onclick="firstPage()">首页</button>
    <button class="button" onclick="prePage()">上一页</button>
    <button class="button" onclick="nextPage()">下一页</button>
    <button class="button" onclick="lastPage()">尾页</button>
</div>
<%--一个编辑授课信息的模态框--%>
<div class="modal" id="editModal">
    <%--编辑模态框--%>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>编辑授课信息</h4>
            </div>
            <div class="modal-body">
                <label class="control-label">教师姓名</label>
                <input class="form-control" type="text" id="nameInput" placeholder="请输入教师姓名">
                <label class="control-label">课程</label>
                <input class="form-control" type="text" id="courseInput" placeholder="请输入课程">
                <label class="control-label">教室</label>
                <input class="form-control" type="text" id="roomInput" placeholder="请输入教室">
                <label class="control-label">上课时间</label>
                <input class="form-control" type="text" id="dateInput" placeholder="请输入上课时间">
                <input type="hidden" id="idInput">
            </div>
            <div class="modal-footer">
                <button class="button" data-dismiss="modal">取消</button>
                <button class="button" onclick="editConfirm()">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    //当前页
    var currentPage = 1;
    //每页记录个数
    var pageSize = 2;
    //总共有多少页
    var totalPage ;

    $(function () {
        //loadList();
        loadListByPage();
    });
    //用户修改的每页展示个数
    function onSelectChange() {
        pageSize = $("#pageSizeSelect").val();
        loadListByPage();
        currentPage = 1;
    }
    function Allpage() {
        pageSize = $("#currentPageSpan").val();
        loadListByPage();
    }
    //当前第几页
    function onChange() {
        totalPage = $("#currentPageSpan").val()
    }
    //首页回调方法
    function firstPage() {
        currentPage = 1;
        loadListByPage();
    }
    /*上一页的回调方法*/
    function prePage() {
        currentPage --;
        if (currentPage < 1){
            currentPage = 1;
        }
        loadListByPage();
    }
    //下一页回调方法
    function nextPage() {
        currentPage++;
        if (currentPage > totalPage){
            currentPage = totalPage;
        }
        loadListByPage();
    }
    //尾页回调方法
    function lastPage() {
        currentPage = totalPage;
        loadListByPage();
    }
    //授课信息列表
    function showTeachInfo(teachInfoList) {
        var html ='';
        for (var i=0;i<teachInfoList.length;i++){
            var item = teachInfoList[i];
            console.log(JSON.stringify(item));
            html+="<tr>";
            html+="<td>"+item.id+"</td>";
            html+="<td>"+item.teacher.name+"</td>";
            html+="<td>"+item.course.name+"</td>";
            html+="<td>"+item.room.location+"</td>";
            html+="<td>"+transfer(new Date(item.date))+"</td>";
            html+="<td><button class='button' onclick='getTeachInfoDetail(\"" + item.id + "\")'>编辑</button>"+
                "<button class='button' onclick='deleteOneTeachInfo(\"" + item.id + "\")' >删除</button></td>"
            html+"</tr>";
        }
        $("#tb").html(html);
    }

    function transfer(date){
        var hours = date.getHours()
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return hours+":"+minute+":"+second;
    }

    //加载、刷新授课信息数据
    function loadList() {
        $.ajax({
            url:"${pageContext.request.contextPath}/teachInfo/getteachInfo",
            type:'get',
            dataType:'json',
            success:function (result) {
                if (result.code == 0){
                    showTeachInfo(result.teachInfoList);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    //分页加载授课列表数据
    function loadListByPage() {
        $.ajax({
            url:"${pageContext.request.contextPath}/teachInfo/getListBypage" ,
            type:'get',
            data:{"currentPage":currentPage,"pageSize":pageSize},
            dataType:'json',
            success:function (result) {
                if (result.code == 0){
                    showTeachInfo(result.teachInfoList);//分页数据
                    totalPage = result.totalPage;//总共有多少页
                    $("#currentPageSpan").html(currentPage);
                    $("#totalPageSpan").html(totalPage);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    //添加、编辑模态框
    function showEditWindow(teachInfo) {
        if(teachInfo!=null){
            console.log(JSON.stringify(teachInfo));
            $("#idInput").val(teachInfo.id);
            $("#nameInput").val(teachInfo.teacher.name);
            $("#courseInput").val(teachInfo.course.name);
            $("#roomInput").val(teachInfo.room.location);
            $("#dateInput").val(teachInfo.date);
        }else {
            //数据全部归零
            $("#idInput").val(0)
            $("#nameInput").val("");
            $("#courseInput").val("");
            $("#roomInput").val("");
            $("#dateInput").val("");
        }
        $("#editModal").modal("show");
    }
    //编辑授课信息
    function getTeachInfoDetail(id) {
        $.ajax({
            url:"${pageContext.request.contextPath}/teachInfo/getone",
            type:'get',
            data:{"id":id},
            dataType:'json',
            success:function (result) {
                if (result.code == 0){
                    showEditWindow(result.teachInfoList)
                }else {
                    alert(result.message);
                }
            } ,
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    //确认按钮
    function editConfirm() {
        var idValue = $("#idInput").val();
        var nameValue = $("#nameInput").val();
        var courseValue = $("#courseInput").val();
        var locationValue = $("#roomInput").val();
        var dateVlaue = $("#dateInput").val();

        var teacherNameValue={name:nameValue};
        var CourseNameValue={name:courseValue};
        var roomLocationValue={location:locationValue};

        var datesource = {id:idValue,teacher:teacherNameValue,course:CourseNameValue,room:roomLocationValue,date:dateVlaue};
        $.ajax({
            url:"${pageContext.request.contextPath}/teachInfo/edit",
            type:'post',
            contentType:'application/json',
            data:JSON.stringify(dataValue), //传递用户编辑后的信息,这里必须是字符串
            dataType:'json',
            success:function (result) {
                if (result.code == 0){
                    $("#editModal").modal("hide");
                    alert(result.message);
                    loadListByPage();
                }else {
                    alert(result.message);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    //添加授课信息
    function addTeachInfo() {
         showEditWindow();
    }
</script>
</body>
</html>
